<?php
/**
 * Created by PhpStorm.
 * User: Kiriti
 * Date: 2016/4/1 0001
 * Time: 14:57
 */

namespace app\controllers;

use yii\web\Controller;
use app\models\Rs_raised;
use app\models\Pr_support;
use Yii;
use app\models\Uc_user;
use app\models\Pr_project;
use app\models\Rs_like;
use app\models\Pr_like;
use app\models\Uc_msg;



class PrlikeController extends Controller{
	
//    public $layout=false;
    public function actionShow(){

        if(!empty($_COOKIE['user_id'])){
            $user_id = $_COOKIE['user_id'];

            //投资数量 关注数量 发起数量
            $user = Uc_user::find()->where([
                'user_id' => $user_id,
                'del_flag' => 0
            ])->one();

            $view = Yii::$app->view;
            $view->params['title'] = "关注项目";
            $this->setView($user);


            $query = Rs_like::find()
                ->where([
                    'user_id' => $user_id, //user_id => user_id
                    'del_flag' => 0
                ])->all();
            $inList = "";
            foreach($query as $key=>$id){
                $inList .= $id['rs_id'];
                $inList .= ",";
            }

            if($inList === ""){
                $inList .= -1;
            }else{
                $inList = substr($inList, 0, -1);
            }
            // echo $inList;
            $raised = Rs_raised::findBySql('SELECT * FROM rs_raised WHERE raised_id in ('. $inList.')')->all();


            $query = Pr_like::find()
                ->where([
                    'user_id' => $user_id, //user_id => user_id
                    'del_flag' => 0
                ])->all();
            $inList = "";
            foreach($query as $key=>$id){
                $inList .= $id['project_id'];
                $inList .= ",";
            }

            if($inList === ""){
                $inList .= -1;
            }else{
                $inList = substr($inList, 0, -1);
            }
            // echo $inList;
            $projects = Pr_project::findBySql('SELECT * FROM rs_raised WHERE raised_id in ('. $inList.')')->all();


            return $this->render('prsupport.php', ['raised'=>$raised, 'projects'=>$projects]);

        }else{
            echo "<script>alert('cookie 不存在')</script>";
            echo "<script>location.href='http://localhost/chuangwu/web/index.php?r=login/show'</script>";
        }

	}

    public function setView($user){
        $view = Yii::$app->view;
        $view->params['user_name'] = $user['user_name'];
        $view->params['field'] = $user['field'];
        $view->params['address'] = $user['address'];


        $query = Pr_support::find()
            ->where([
                'user_id' => $user['user_id'],
                'del_flag' => 0
            ])->all();
        $inList = "";
        foreach($query as $key=>$id){
            $inList .= $id['raised_id'];
            $inList .= ",";
        }

        if($inList === ""){
            $inList .= -1;
        }else{
            $inList = substr($inList, 0, -1);
        }

        $raised = Rs_raised::findBySql('SELECT * FROM rs_raised WHERE raised_id in ('. $inList.')')->all();

        $view->params['invest_num'] = count($raised);

        $launch1 = Pr_project::find()
            ->where([
                'user_id' => $user['user_id'], //user_id => user_id
                'del_flag' => 0
            ])->count();
        $launch2 = Rs_raised::find()
            ->where([
                'user_id' => $user['user_id'], //user_id => user_id
                'del_flag' => 0
            ])->count();
        $view->params['launch_num'] = $launch1+$launch2;

        $count = Uc_msg::find()
            ->where([
                'to_user' => $user['user_id'], //user_id => user_id
                'del_flag' => 0,
                'is_read' =>0
            ])->count();
        $view->params['msgCount'] = $count;


        $query = Rs_like::find()
            ->where([
                'user_id' => $user['user_id'], //user_id => user_id
                'del_flag' => 0
            ])->all();
        $inList = "";
        foreach($query as $key=>$id){
            $inList .= $id['rs_id'];
            $inList .= ",";
        }

        if($inList === ""){
            $inList .= -1;
        }else{
            $inList = substr($inList, 0, -1);
        }
        // echo $inList;
        $raised = Rs_raised::findBySql('SELECT * FROM rs_raised WHERE raised_id in ('. $inList.')')->all();

        $like1 = count($raised);

        $query = Pr_like::find()
            ->where([
                'user_id' => $user['user_id'], //user_id => user_id
                'del_flag' => 0
            ])->all();
        $inList = "";
        foreach($query as $key=>$id){
            $inList .= $id['project_id'];
            $inList .= ",";
        }

        if($inList === ""){
            $inList .= -1;
        }else{
            $inList = substr($inList, 0, -1);
        }
        // echo $inList;
        $raised = Rs_raised::findBySql('SELECT * FROM pr_project WHERE pr_id in ('. $inList.')')->all();

        $like2 = count($raised);
        $view->params['like_num'] = $like1 + $like2;

//        echo "<script>alert(1)</script>";
        $view->params['headImage'] = $user['headImage'];
//        $view->params['headImage'] = '';



    }
}